package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_dingdan;

import cn.com.api.service.ApiT_dingdanService;

import cn.com.util.*;
import cn.com.util.*;

public class T_dingdanBaseController {

	private static Logger logger = Logger.getLogger(T_dingdanBaseController.class);

	@Autowired
	private ApiT_dingdanService apiT_dingdanService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_dingdan-list")
	public ModelAndView t_dingdan_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_dingdan_list.jsp");

		List<ApiT_dingdan> t_dingdans = apiT_dingdanService.findT_dingdans();
		mv.addObject("t_dingdans", t_dingdans);

		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_dingdan-add-pre")
	public ModelAndView t_dingdan_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_dingdan_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiT_dingdan t_dingdan = apiT_dingdanService.findT_dingdanById(Integer.valueOf(id));
			mv.addObject("t_dingdan", t_dingdan);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_dingdan-add")
	public void t_dingdan_add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String rid = request.getParameter("rid");
		String titles = request.getParameter("titles");
		String shop_id = request.getParameter("shop_id");
		String uid = request.getParameter("uid");
		String peisong = request.getParameter("peisong");
		String name = request.getParameter("name");
		String tel = request.getParameter("tel");
		String mobile = request.getParameter("mobile");
		String email = request.getParameter("email");
		String zip = request.getParameter("zip");
		String address = request.getParameter("address");
		String dtime = request.getParameter("dtime");
		String bz = request.getParameter("bz");
		String money = request.getParameter("money");
		String appuser = request.getParameter("appuser");
		String appdate = request.getParameter("appdate");
		String rip = request.getParameter("rip");
		String enable = request.getParameter("enable");
		String zi_status = request.getParameter("zi_status");
		String dd_status = request.getParameter("dd_status");
		String zf_type = request.getParameter("zf_type");
		String remainmoney = request.getParameter("remainmoney");
		String serial = request.getParameter("serial");
		String serialflagno = request.getParameter("serialflagno");
		String ordertype = request.getParameter("ordertype");
		String paytime = request.getParameter("paytime");
		String sumgoodsprice = request.getParameter("sumgoodsprice");
		String sendfee = request.getParameter("sendfee");
		String takegoodstime = request.getParameter("takegoodstime");
		String cancelreason = request.getParameter("cancelreason");
		String product_ids = request.getParameter("product_ids");
		String car_ids = request.getParameter("car_ids");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiT_dingdan t_dingdan = new ApiT_dingdan();

			// t_dingdan.setRid(rid);
			t_dingdan.setTitles(titles);
			t_dingdan.setShop_id(shop_id);
			t_dingdan.setUid(uid);
			t_dingdan.setPeisong(peisong);
			t_dingdan.setName(name);
			t_dingdan.setTel(tel);
			t_dingdan.setMobile(mobile);
			t_dingdan.setEmail(email);
			t_dingdan.setZip(zip);
			t_dingdan.setAddress(address);
			t_dingdan.setDtime(dtime);
			t_dingdan.setBz(bz);
			// t_dingdan.setMoney(Float.valueOf(money));
			t_dingdan.setAppuser(appuser);
			t_dingdan.setCancelreason(cancelreason);
			t_dingdan.setProduct_ids(product_ids);
			t_dingdan.setCar_ids(car_ids);

			apiT_dingdanService.insertT_dingdan(t_dingdan);
		} else {// edit
			ApiT_dingdan t_dingdan = apiT_dingdanService.findT_dingdanById(Integer.valueOf(id));

			t_dingdan.setTitles(titles);
			t_dingdan.setUid(uid);
			t_dingdan.setPeisong(peisong);
			t_dingdan.setName(name);
			t_dingdan.setTel(tel);
			t_dingdan.setMobile(mobile);
			t_dingdan.setEmail(email);
			t_dingdan.setZip(zip);
			t_dingdan.setAddress(address);
			t_dingdan.setDtime(dtime);
			t_dingdan.setBz(bz);
			t_dingdan.setAppuser(appuser);
			t_dingdan.setRip(rip);
			t_dingdan.setEnable(Integer.valueOf(enable));
			t_dingdan.setZi_status(Integer.valueOf(zi_status));
			t_dingdan.setDd_status(Integer.valueOf(dd_status));
			t_dingdan.setZf_type(zf_type);

			t_dingdan.setSerial(serial);
			t_dingdan.setSerialflagno(serialflagno);

			t_dingdan.setCancelreason(cancelreason);
			t_dingdan.setProduct_ids(product_ids);
			t_dingdan.setCar_ids(car_ids);

			apiT_dingdanService.updateT_dingdan(t_dingdan);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_dingdan-delete")
	public void t_dingdan_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiT_dingdanService.deleteT_dingdan(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
